import { defineStore } from "pinia";
import { ref, computed, nextTick } from "vue";
import { getSystemInfo } from "@/utils/tool";
/**
 * app仓库
 */
export const useAppStore = defineStore(
  "appStore",
  () => {
    //   系统信息
    const systemInfo = ref(null);
    //   更新系统信息
    const updateSystemInfo = async () => {
      const res = await getSystemInfo();
      systemInfo.value = res;
      return res;
    };
    //   小程序信息
    const miniProgram = ref(null);
    // 更新小程序信息
    const updateMiniProgram = () => {
      miniProgram.value = wx.getAccountInfoSync().miniProgram;
      return Promise.resolve(miniProgram.value);
    };
    // 适配底部安全区域
    const safeAreaInsetsBottom = computed(() => {
      return systemInfo.value?.safeAreaInsets.bottom || 0;
    });
    // 自定义导航条高度
    const customBarHeight = computed(() => {
      return systemInfo.value?.customBarHeight || 0;
    });
    // 状态栏高度
    const statusBarHeight = computed(() => {
      return systemInfo.value?.statusBarHeight || 0;
    });



    return {
      systemInfo, 
      updateSystemInfo,
      miniProgram,
      updateMiniProgram,
      safeAreaInsetsBottom,
      customBarHeight,
      statusBarHeight,
     
    };
  },
  {
    // 指定state数据持久化
    unistorage: {
      key: 'appStore',
      paths: ["miniProgram", "systemInfo"]
    },
  }
);
